<template>
  <el-dialog v-model="visible" :width="'30%'" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item label="版本类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="版本类型">
          <el-option
            v-for="item in dataForm.typeOp"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="版本名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="版本名称"></el-input>
      </el-form-item>
      <el-form-item label="版本号" prop="number">
        <el-input v-model="dataForm.number" placeholder="版本号"></el-input>
      </el-form-item>
      <el-form-item label="文件上传" prop="fileurl">
        <el-upload
          class="avatar-uploader"
          :action="url"
          :show-file-list="false"
          :on-success="successHandle"
          :before-upload="beforeUploadHandle">
          <i class="el-icon-folder"  v-if="dataForm.fileurl">
            {{dataForm.number+".apk"}}
          </i>
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="是否强制更新" prop="isupdate">
        <el-select v-model="dataForm.isupdate" placeholder="是否强制更新">
          <el-option
            v-for="item in dataForm.isupdateOp"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="更新信息" prop="notes">
        <el-input
          type="textarea"
          placeholder="请输入更新信息"
          v-model="dataForm.notes"
          maxlength="100"
          show-word-limit
          size="large"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";
import app from "@/constants/app";
import {getToken} from "@/utils/cache";
import {IObject} from "@/types/interface";

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: '', type: '', name: '', number: '', fileurl: '', isupdate: '',  createDate: '',
  fileName:"",notes:"",
  typeOp:[{label: "安卓版", value: 1}, {label: "苹果版本", value: 2}],
  isupdateOp:[{label: "是", value: 1}, {label: "否", value: 2}]
});

const fileList = ref<IObject[]>();
const fileUrl = ref();
const rules = ref({
  type: [{required: true, message: '版本类型不能为空', trigger: 'blur'}],
  name: [{required: true, message: '版本名称不能为空', trigger: 'blur'}],
  number: [{required: true, message: '版本号不能为空', trigger: 'blur'}],
  fileurl: [{required: true, message: '文件上传不能为空', trigger: 'blur'}],
  isupdate: [{required: true, message: '是否强制更新不能为空', trigger: 'blur'}],
});
const url = ref("");
const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";
  dataForm.fileName="";
  url.value = `${app.api}/sys/oss/uploadByLocal?token=${getToken()}`;
  fileUrl.value = `${app.api}/sys/oss/downloadLocal`;
  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }
  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/sysapp/edition/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/sysapp/edition", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

const num =  ref(0);
// 上传之前
const beforeUploadHandle = (file: IObject) => {
  if (file.type !== "application/vnd.android.package-archive") {
    ElMessage.error("只支持apk格式文件！");
    return false;
  }
  num.value++;
};
// 上传成功
const successHandle = (res: IObject, file: IObject, list: IObject[]) => {
  if (res.code !== 0) {
    return ElMessage.error(res.msg);
  }

  ElMessage.success({
    message: "文件上传成功",
    duration: 500,
    onClose: () => {
      let url =fileUrl.value+"?name="+res.data.url;
      dataForm.fileurl=url;
      dataForm.fileName = file.name;
    }
  });
};

defineExpose({
  init
});
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
